﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CRM
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Common.SetMes.RemoveSession("User_ID");
                Common.SetMes.CreatCookies("User_ID", -1, "", "");
                Common.SetMes.CreatCookies("Power_Mes", -1, "", "");
                Common.SetMes.CreatCookies("User_Name", -1, "", "");
                Common.SetMes.CreatCookies("Power_Name", -1, "", "");
            }
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            if (CheckNull())                  //检查输入框是否为空
            {  
                //如果用户输入了正确的验证码，则进行身份验证
                if (Common.GetMes.GetCheckResult
                    ("CheckCode", txtU_Check.Value.Trim().ToLower()))
                {
                    //调用业务逻辑层的UserLogin方法对用户名和密码进行验证
                    SqlDataReader DataRead = 
                        BLL.ServiceUser.UserLogin
                        (txtU_Name.Value.Trim(), txtU_Pass.Value.Trim());
                    if (DataRead.HasRows)    //如果找到匹配的用户名和密码
                    {
                        DataRead.Read();     //读取SqlDataReader中的下一条记录
                        //在Session中保存用户ID
                        Session["User_ID"] = DataRead["T_ServiceID"].ToString();
                        //创建用户ID、用户名、角色权限和角色名的Cookie值
                        Common.SetMes.CreatCookies("User_ID", 4, 
                            DataRead["T_ServiceID"].ToString(), "");
                        Common.SetMes.CreatCookies("User_Name", 10, 
                            Common.EncryptString.MyEncrypt
                            (txtU_Name.Value.Trim(), true), "");
                        Common.SetMes.CreatCookies("Power_Mes", 15, 
                            Common.EncryptString.MyEncrypt
                            (DataRead["T_PowerMes"].ToString(),true), "");
                        Common.SetMes.CreatCookies("Power_Name", 15, 
                            Common.EncryptString.MyEncrypt
                            (DataRead["T_PowerName"].ToString(),true),"");
                        //从Session中移除验证码
                        Common.SetMes.RemoveSession("CheckCode");
                        Response.Redirect("Main.aspx");  //重定向到Main.aspx页面
                    }
                    else
                    {
                        //显示错误消息后重定向到Index.aspx页面
                        Common.MessageBox.Show
                            (Page, "用户名密码错误或该账户被禁用..", 
                            "Index.aspx", "Error1");
                    }
                    DataRead.Close();       //关闭并释放SqlDataReader
                    DataRead.Dispose();
                }
                else
                { 
                    //验证码错误提示重新输入验证码
                    Common.MessageBox.Show
                        (Page, "验证码有误，请看清图片上的数字..", 
                        "Index.aspx", "Error2");
                }
            }
            else
            {
                //提示用户输入完整的登录信息
                Common.MessageBox.Show(Page, "登录信息不完整..",
                    "Index.aspx", "Error3");
            }
        }
        /// <summary>
        /// 空验证
        /// </summary>
        /// <returns></returns>
        private bool CheckNull()
        {
            //5+1+a+s+p+x
            return (!(string.IsNullOrEmpty(txtU_Name.Value.Trim())) && !(string.IsNullOrEmpty(txtU_Pass.Value.Trim())) && !(string.IsNullOrEmpty(txtU_Check.Value.Trim()))) ? true : false;
        }
    }
}
